<template>
  <div class="content">
    <div class="top">
      <div class="fl" @click="goToBack">返回</div>
      <div class="fr" @click="goToDetail">我的邀新</div>
    </div>
    <div class="middle">
      <div class="extend_title">
        <img src="/static/wechat/images/extend_title.png" alt="">
      </div>
      <div class="extend_code">
          <div class="code_text">
            <p>我的邀新码</p>
            <p>{{code}}</p>
          </div>
          <el-button class="code_button" type="text" size="medium" name="copy"
        v-clipboard:copy="code"
        v-clipboard:success="onCopy"
        v-clipboard:error="onError">复制</el-button>
      </div>
      <div class="extend_people">
         <img src="/static/wechat/images/extend_people.png" alt="">
      </div>
      <div class="extend_build">
         <img src="/static/wechat/images/extend_build.png" alt="">
      </div>
    </div>
    <div class="bottom">
      <div class="rule_title">
        <img src="/static/wechat/images/extend_rule.png" alt="">
      </div>
      <div class="rule_content">
        <p>1.分享给好友邀新码，让好友注册时填写你的邀新码完成注册,</p>
        <p>2.好友注册成功后完成入职，你将得到{{spreadBasic}}元奖励,</p>
        <p>3.好友入职45天后，你将得到{{spreadAfterWork}}元,</p>
        <p>4.推荐越多奖励越多:</p>
        <p v-for="(item, index) in rewardList" class="list" :key='item.id'>
          <span v-if="index<rewardList.length-1">
            &nbsp;&nbsp;{{messageArr[index]}}~{{messageArr[index]+item.spreadCount-1}}个用户每人推荐费{{item.spreadMoney}};
          </span>
          <span v-if="index==rewardList.length-1">
            &nbsp;&nbsp;{{messageArr[index]}}~{{messageArr[index]+item.spreadCount-1}}个用户每人推荐费{{item.spreadMoney}}。
          </span>
        </p>
      </div>
    </div>
    <bottomMenu :menuType="menuType"></bottomMenu>
  </div>
</template>


<script>
  import Vue from 'vue'
  import VueClipboard from 'vue-clipboard2'
  Vue.use(VueClipboard)
  import {getCode} from '@/controller'
  import {getCodeRule} from '@/controller'
  import bottomMenu from './bottomMenu.vue'

  export default {
    name: 'exend',
    data () {
      return {
        code:'',
        menuType:1, // 1代表推荐
        spreadBasic:0,
        spreadAfterWork:0,
        rewardList:[]
      }
    },
    components: {
      'bottomMenu': bottomMenu
    },
    computed:{
      messageArr:function(){
            var arr=[];
            var arr1=this.rewardList;
            function compare(property){
              return function(a,b){
                  var value1 = a[property];
                  var value2 = b[property];
                  return value1 - value2;
              }
            }
            this.rewardList=arr1.sort(compare('spreadLevel'));
            for(var i=0;i<this.rewardList.length-1;i++){
                if(this.rewardList[i].spreadLevel===0){
                    arr[i]=this.rewardList[i].spreadLevel;
                    arr[i+1]=arr[i]+this.rewardList[i].spreadCount;
                }
                else{
                    arr[i+1]=arr[i]+this.rewardList[i].spreadCount;
                }
            }
            return arr;
        },
    },
    methods: {
      init() {
        //请求推荐码
        getCode({
        }).then((res) => {
          if (res.code === 0) {
            this.code=res.data;
          } else {
            this.$toast.center('鉴权失败');
          }
        });
        getCodeRule({}).then((res) => {
          if (res.code === 0) {
            this.spreadBasic=res.data.rule.spreadBasic;
            this.spreadAfterWork=res.data.rule.spreadAfterWork;
            this.rewardList=res.data.ruleDetail;
          } else {
            this.$toast.center('鉴权失败');
          }
        })
      },
      goToDetail(){
        this.$router.push({
          path: `/extendDetail`,
        });
      },
      goToBack(){
        this.$router.push({
          path: `/`
        });
      },
      // 复制成功
      onCopy(e){
        this.$toast.top('复制成功');
      },
      // 复制失败
      onError(e){
         this.$toast.top('复制失败');
      },
    },
    mounted() {
      this.init();
    },
  }

</script>
<style scoped>
  .content{
    width: 100%;
    position: fixed;
    border-radius: 0;
    height: 100%;
    overflow-y: scroll;
    background: url(/static/wechat/images/extend_bg.jpg) no-repeat scroll center center;
    background-size: cover;
  }
  ::-webkit-scrollbar {
        display: none;
    }
  .content img{
    width: 100%;
  }
  .top{
    width: 90%;
    margin: 0 auto;
    overflow: hidden;
    color: #fff;
    padding: 2% 0;
  }
  .middle{
    position: relative;
    width: 100%;
    padding: 5% 0;
  }
  .middle .extend_title{
    width: 82%;
    margin: 0 auto 10%;
  }
  .middle .extend_code{
    width: 52%;
    margin: 5% auto;
    font-size: 0;
    display: flex;
    align-items: center;
    height: 60px;
  }
  .extend_code .code_text, .extend_code .code_button{
    display: inline-block;
    vertical-align: middle;
    padding: 5% 0;
    text-align: center;
    font-size: 12px;
    position: relative;
  }
  .extend_code .code_text{
    background: #ffb517;
    width: 68%;
    height: 60px;
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
    color:#fff;
    font-size: 14px;
    line-height: 30px;
  }
  .code_text p:last-child{
    font-size: 24px;
    font-weight: bold;
  }
  .code_text:before{
    position: absolute;
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #1d84e0;
    top: -3px;
    right: -3px;
  }
  .code_text:after{
    position: absolute;
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #1d84e0;
    bottom: -3px;
    right: -3px;
  }
  .extend_code .code_button{
    background: #fff;
    width: 32%;
    height: 60px;
    line-height: 36px;
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;
    line-height: 60px;
    color: #ff4f56;
    font-size: 16px;
    font-weight: bold;
  }
  .code_button:before{
    position: absolute;
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #1d84e0;
    top: -3px;
    left: -3px;
  }
  .code_button:after{
    position: absolute;
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #1d84e0;
    bottom: -3px;
    left: -3px;
  }
  .middle .extend_people{
    position: absolute;
    width: 45%;
    bottom: -14%;
    left: -5%;
    z-index: -1;
  }
  .middle .extend_build{
    position: absolute;
    width: 70%;
    bottom: -10%;
    right: -16%;
    z-index: -1;
  }
  .bottom{
    width: 92%;
    padding: 4%;
    background: #fff;
    margin: 10% auto 18%;
    border-radius: 6px;
    box-sizing: border-box;
  }
  .bottom .rule_title{
    width: 36%;
    margin:0 auto;
  }
  .bottom .rule_content{
    width: 98%;
    color: #666666;
    font-size: 14px;
    line-height: 24px;
  }
  .list{
    text-indent:4px;
  }
</style>
